local MeleeSkillOnHead = MClass("MeleeSkillOnHead")
local battleCfg = battle.BattleConfig:getInstance()


function MeleeSkillOnHead:ctor()
    --self.root 默认为根节点
    --设置事件
    self.icon1 = self:findChild("Panel/icon_1")
    self.icon2 = self:findChild("Panel/icon_2")
	self.num_bg = self:findChild("Panel/num_bg")
	self.num = self:findChild("Panel/num_bg/num")
    self:init()
end

function MeleeSkillOnHead:init()
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.MELEE_SKILL,MeleeSkillOnHead.setProp)
	BattleUIEvent:addEventListener(BattleUIEvent.TYPE.BATTLE_REFRESH_FLOWER_NODE, MeleeSkillOnHead.setAdditionNode)
end

function MeleeSkillOnHead:setProp(skills,unit)
	if not self.icon then
		return
	end

	if #skills > 0 then
		self:setVisible(true)
		self.icon:setVisible(true)
		self.cfg = battleCfg:getItemConfig(unit:getItemIdByInstanceID(skills[1]))
		self.iconPic = self.cfg:getItemIcon()
		self.icon:loadTexture(string.format(Res.meleeIconPath, self.iconPic))
	else
		self:setVisible(false)
		self.icon:setVisible(false)
		self.state = false
	end
end

function MeleeSkillOnHead:setLeft(bLeft)
	if bLeft then
		self.icon = self.icon1
	else
		self.icon = self.icon2
	end
end

function MeleeSkillOnHead:setNodeVisible(aVisible)
	self:setVisible(aVisible)
	self.icon:setVisible(aVisible)
	self.num_bg:setVisible(aVisible)
end

function MeleeSkillOnHead:setAdditionNode(num)
	if not self.icon then
		return
	end
	
	if num > 0 then
		self:setNodeVisible(true)		
		local cfg = battleCfg:getItemConfig(Constant.Zombies_Moon_Flower_ID_In_Config)
		self.icon:loadTexture(string.format(Res.ZombieShopIcon, cfg:getItemIcon()))				
		local lvStr = "X" .. tostring(num)
		self.num:setString(lvStr)
	else
		self:setNodeVisible(false)	
	end
	
end

return MeleeSkillOnHead